package hah;

import java.util.Stack;

public class nibolanCalculate {

        public int evalRPN(String[] tokens) {
            Stack<Integer> stack =new Stack<>();
            for (String s :tokens){
                if (s.equals("+")){
                    int a=stack.pop();
                    int b=stack.pop();
                    stack.push(a+b);
                }else if (s.equals("-")){
                    int a=stack.pop();
                    int b=stack.pop();
                    stack.push(b-a);
                }else if (s.equals("*")){
                    int a=stack.pop();
                    int b=stack.pop();
                    stack.push(a*b);
                }else if (s.equals("/")){
                    int a=stack.pop();
                    int b=stack.pop();
                    stack.push(b/a);
                }else {
                    stack.push(Integer.parseInt(s));
                }
            }
            return stack.peek();
        }

        public int lengthOfLastWord2(String s) {
            String[] s1 = s.split(" ");
            if (s1.length==0){
                return 0;
            }else {
                return s1[s1.length-1].length();
            }
        }
        public int lengthOfLastWord(String s) {
            int end=s.length()-1;
            while(end>=0&&s.charAt(end)==' ') end--;
            int start=end;
            while(start>=0&&s.charAt(start)!=' ')start--;
            return end-start;
    }
        public int[] plusOne(int[] digits) {
            int d=0;
            for (int i= digits.length-1;i>=0;i--){
                if (i!= digits.length-1){
                    digits[i]=digits[i]+d;
                    d=digits[i]/10;
                }else {
                    digits[i]++;
                    d=digits[i]/10;
                }
            }
            return digits;
        }

}
